package com.hb.trans.mvp.ui.activity


import android.os.Bundle
import com.google.android.material.tabs.TabLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import android.view.MenuItem
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.base.BaseActivity
import com.hb.trans.R
import com.hb.trans.global.ACTIVITY_COMPANY_DETAIL
import com.hb.trans.mvp.presenter.NewDetailPresenter
import com.hb.trans.mvp.ui.fragment.CompanyDetailItemFragment
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_company_detail.*
import javax.inject.Inject

// 信息详情
@Route(path = ACTIVITY_COMPANY_DETAIL)
class CompanyDetailActivity : BaseActivity() {
    @Inject
    override fun getContentViewResId(): Int = R.layout.activity_company_detail

    override fun onActivityInject() {
        AndroidInjection.inject(this)
    }

    override fun attachView(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
    }

    override fun getPresenter(): NewDetailPresenter? = null

    override fun initView() {
        setSupportActionBar(companyDetailToolbar.toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.setHomeAsUpIndicator(R.mipmap.ic_left_arrow)
        initTab()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> onBackPressed()
        }
        return true
    }

    override fun initData() {
    }

    override fun setListener() {
    }

    private fun initTab() {
        var mTitle = listOf("企业基本信息", "企业经营信息")

        var fragment1 = CompanyDetailItemFragment()
        var bundle1 = Bundle()
        bundle1.putInt("page", 1)
        fragment1.setArguments(bundle1)

        var fragment2 = CompanyDetailItemFragment()
        var bundle2 = Bundle()
        bundle2.putInt("page", 2)
        fragment2.setArguments(bundle2)



        var mFragment = listOf(
            fragment1, fragment2
        )
        companyDetailTabLayout.addTab(companyDetailTabLayout.newTab().setText(mTitle[0]))
        companyDetailTabLayout.addTab(companyDetailTabLayout.newTab().setText(mTitle[1]))
        companyDetailTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                //选中Tab
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
                //未选中Tab
            }

            override fun onTabReselected(tab: TabLayout.Tab) {
                //再次选中
            }
        })

        companyDetailViewPager.adapter = object :
            androidx.fragment.app.FragmentPagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): androidx.fragment.app.Fragment {
                return mFragment[position]
            }

            override fun getCount(): Int {
                return mFragment.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return mTitle[position]
            }
        }
        companyDetailTabLayout.setupWithViewPager(companyDetailViewPager)
//        companyDetailTabLayout.getTabAt(2)!!.select()

    }
}
